package com.example.demo.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
* @Package: com.example.demo.filter
* @ClassName: LogFilter
* @Author: Think
* @CreateTime: 2021/7/12 13:33
* @Description:
*/

@Component
public class GlobalLogFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
    // 前置过滤器
    System.out.println("GlobalLogFilter filtered!--PRE");
    return chain.filter(exchange).then(
            Mono.fromRunnable(() -> {
                        // 后置过滤器
                        System.out.println("GlobalLogFilter filtered!--POST");
                    }
            ));
}

/**
 * 设定过滤器的优先级，值越小则优先级越高
 *
 * @return
 */
@Override
public int getOrder() {
    return 0;
}
}
